/*
*  Copyright 2019-2025 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.sqlservice.impl;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import me.zhengjie.mapper.WcsInboundMissionSqlMapper;
import me.zhengjie.pojo.dto.WcsInboundMissionQueryCriteria;
import me.zhengjie.pojo.entity.WcsInboundMission;
import me.zhengjie.sqlservice.WcsInboundMissionSqlService;
import me.zhengjie.utils.FileUtil;
import me.zhengjie.utils.PageResult;
import me.zhengjie.utils.PageUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;


/**
* @description 服务实现
* @author zhixiao
* @date 2025-06-28
**/
@Service
@RequiredArgsConstructor
public class WcsInboundMissionSqlServiceImpl extends ServiceImpl<WcsInboundMissionSqlMapper, WcsInboundMission> implements WcsInboundMissionSqlService {

    private final WcsInboundMissionSqlMapper wcsInboundMissionMapper;

    @Override
    public PageResult<WcsInboundMission> queryAll(WcsInboundMissionQueryCriteria criteria, Page<Object> page){
        return PageUtil.toPage(wcsInboundMissionMapper.findAll(criteria, page));
    }

    @Override
    public List<WcsInboundMission> queryAll(WcsInboundMissionQueryCriteria criteria){
        return wcsInboundMissionMapper.findAll(criteria);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(WcsInboundMission resources) {
        wcsInboundMissionMapper.insert(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(WcsInboundMission resources) {
        WcsInboundMission wcsInboundMission = getById(resources.getId());
        wcsInboundMission.copy(resources);
        wcsInboundMissionMapper.updateById(wcsInboundMission);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<Long> ids) {
        wcsInboundMissionMapper.deleteBatchIds(ids);
    }

    @Override
    public void download(List<WcsInboundMission> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (WcsInboundMission wcsInboundMission : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("任务编码", wcsInboundMission.getInboundCode());
            map.put("任务状态", wcsInboundMission.getMissionStatus());
            map.put("容器编码", wcsInboundMission.getContainerCode());
            map.put("任务起点", wcsInboundMission.getSource());
            map.put("任务终点", wcsInboundMission.getDestination());
            map.put("创建时间", wcsInboundMission.getCreateTime());
            map.put("更新时间", wcsInboundMission.getUpdateTime());
            map.put("agvID", wcsInboundMission.getAgvId());
            map.put("容器类型", wcsInboundMission.getContainerType());
            map.put("入库类型", wcsInboundMission.getInboundType());
            map.put("优先级", wcsInboundMission.getPriority());
            map.put("任务类型", wcsInboundMission.getTaskType());
            map.put("是否回库嵌套（默认0,1是嵌套）", wcsInboundMission.getIsNest());
            map.put("0默认 1plc", wcsInboundMission.getIsPlc());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}